package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Attendance;
import cn.wolfcode.crm.query.AttendanceQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IAttendanceService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("attendance")
public class AttendanceController {
    @Autowired
    private IAttendanceService attendanceService;

    @RequiresPermissions(value = {"attendance:view", "考勤视图"}, logical = Logical.OR)
    @RequestMapping("view")
    public String view() {
        return "attendance/attendance";
    }

    @RequiresPermissions(value = {"attendance:query", "考勤列表"}, logical = Logical.OR)
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(AttendanceQueryObject qo) {
        return attendanceService.query(qo);
    }

    @RequiresPermissions(value = {"attendance:selectAll", "考勤全选"}, logical = Logical.OR)
    @RequestMapping("selectAll")
    @ResponseBody
    public List<Attendance> selectAll() {
        return attendanceService.selectAll();
    }

    @RequestMapping("onState")
    @ResponseBody
    public JsonResult onState() {
        try {
            attendanceService.onState();
        } catch (RuntimeException e) {
            return new JsonResult().mark(e.getMessage());
        }
        return new JsonResult();
    }

    @RequestMapping("downState")
    @ResponseBody
    public JsonResult downState() {
        try {
            attendanceService.downState();
        } catch (RuntimeException e) {
            return new JsonResult().mark(e.getMessage());
        }
        return new JsonResult();
    }

    @RequestMapping("signUp")
    @ResponseBody
    public JsonResult signUp(Date selectDate,Long employeeId) {
        System.out.println(selectDate);
        System.out.println(employeeId);
        try {
            attendanceService.signUp(selectDate,employeeId);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new JsonResult().mark(e.getMessage());
        }
        return new JsonResult();
    }
}
